package br.unifor.cadeventos.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import br.unifor.cadeventos.manager.UsuarioManager;
import br.unifor.cadeventos.utils.DesktopContext;

public class PrincipalEvento extends JFrame {

	private static final long serialVersionUID = 4248943417312423779L;

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;
	private static JInternalFrame internalFrame;
	private JLabel lblTeste;
	private UsuarioManager usuarioManager = new UsuarioManager();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PrincipalEvento frame = new PrincipalEvento();
					DesktopContext.setContextFrame(frame);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @throws PropertyVetoException
	 */
	public PrincipalEvento() throws PropertyVetoException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 672, 464);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		internalFrame = new JInternalFrame("Autenticar");
		internalFrame.setSelected(true);
		internalFrame.setBounds(196, 129, 256, 198);
		contentPane.add(internalFrame, BorderLayout.CENTER);
		internalFrame.getContentPane().setLayout(null);

		textField = new JTextField();
		textField.setBounds(70, 24, 134, 28);
		internalFrame.getContentPane().add(textField);
		textField.setColumns(10);

		JLabel lblLogin = new JLabel("Login:");
		lblLogin.setBounds(19, 30, 61, 16);
		internalFrame.getContentPane().add(lblLogin);

		JLabel lblSenha = new JLabel("Senha:");
		lblSenha.setBounds(19, 70, 61, 16);
		internalFrame.getContentPane().add(lblSenha);

		JButton btnEntrar = new JButton("Entrar");
		btnEntrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String retorno = usuarioManager.logar(textField.getText(),
						String.valueOf(passwordField.getPassword()));
				if (DesktopContext.getUsuarioLogado() != null) {
					internalFrame.dispose();
					internalFrame = null;
					FiltroEvento filtroEvento = new FiltroEvento();
					JLabel labelRetorno = new JLabel(retorno);
					labelRetorno.setBounds(10, 11, 211, 14);
					labelRetorno.setForeground(Color.BLUE);
					filtroEvento.add(labelRetorno);
					setContentPane(filtroEvento);
					filtroEvento.repaint();
					DesktopContext.getContextFrame().repaint();
				} else {
					lblTeste = new JLabel(retorno);
					lblTeste.setForeground(Color.RED);
					lblTeste.setBounds(6, 6, 220, 16);
					internalFrame.getContentPane().add(lblTeste);
					internalFrame.repaint();
				}
			}
		});
		btnEntrar.setBounds(57, 105, 117, 29);
		internalFrame.getContentPane().add(btnEntrar);
		internalFrame.getRootPane().setDefaultButton(btnEntrar);
		btnEntrar.requestFocus();

		passwordField = new JPasswordField();
		passwordField.setBounds(70, 64, 134, 28);
		internalFrame.getContentPane().add(passwordField);
		internalFrame.setVisible(true);

	}
}
